import 'package:Smart/res/app_resources.dart';
import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

class GSDialogUtil {
  /// 一个有取消和确认的Dialog弹框
  static showCancelAndConfirm(BuildContext context,
      {@required String message,
      String cancelText = "取消",
      String confirmText = "确认",
      String imagePath = "",
      bool showCehckbox = false,
      onCancel(),
      onConfirm(bool checkboxValue)}) {
    showDialog(
        context: context,
        // barrierDismissible: false,
        builder: (BuildContext context) {
          return Scaffold(
              backgroundColor: Colors.transparent,
              body: BlocProvider(
                  create: (context) => DialogUtilCubit(),
                  child: BlocBuilder<DialogUtilCubit, DialogUtilState>(
                    builder: (context, state) => Center(
                      child: Container(
                          // color: Colors.green,
                          // constraints: BoxConstraints(
                          //   maxHeight: MediaQuery.of(context).size.height*0.9
                          // ),
                          child: Stack(
                        alignment: Alignment.center,
                        children: [
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 140.w),
                            padding: EdgeInsets.only(
                                left: 20.w, right: 20.w, bottom: 20.w),
                            width: 314.w,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(10.w),
                            ),
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                SizedBox(height: 60.w),
                                _text(message),
                                Container(
                                  height: showCehckbox ? 68.h : 20.h,
                                  // color: Colors.red,
                                  child: showCehckbox
                                      ? Row(
                                          children: [
                                            Checkbox(
                                                value: state.checked,
                                                checkColor: Color(0xFFFFFFFF),
                                                activeColor: Color(0xFF969DA3),
                                                onChanged: (selected) {
                                                  BlocProvider.of<
                                                              DialogUtilCubit>(
                                                          context)
                                                      .onCheckChange();
                                                }),
                                            Expanded(
                                                child: Text(
                                              "下次不再提示",
                                              style: TextStyle(
                                                  color: Color(0xFF595959),
                                                  fontSize: 14.w),
                                            ))
                                          ],
                                        )
                                      : Container(),
                                ),
                                Container(
                                  // color: Colors.yellow,
                                  padding: EdgeInsets.only(top: 20.w),
                                  child: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      _button(cancelText,
                                          width: 130.w,
                                          height: 44.w, onTap: () {
                                        Navigator.of(context).pop();
                                        if (onCancel != null) onCancel();
                                      }),
                                      SizedBox(width: 14.w),
                                      _button(
                                        confirmText,
                                        width: 130.w,
                                        height: 44.w,
                                        selected: true,
                                        onTap: () {
                                          if (onConfirm != null)
                                            onConfirm(state.checked);
                                          Navigator.of(context).pop();
                                        },
                                      )
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                          Positioned(
                            top: 97.5.w,
                            child: imagePath != ""
                                ? Container(
                                    // color: Colors.orange,
                                    alignment: Alignment.center,
                                    child: Image.asset(imagePath,
                                        height: 86.w, width: 86.w),
                                  )
                                : Container(),
                          ),
                          Positioned(
                            bottom: 0,
                            child: GestureDetector(
                              onTap: () {
                                Navigator.of(context).pop();
                              },
                              child: SvgPicture.asset(
                                AppResources.imagePath(
                                  "svgs/close_pop.svg",
                                ),
                                color: Colors.white,
                                width: 45.w,
                                height: 45.w,
                              ),
                            ),
                          )
                        ],
                      )),
                    ),
                  )));
        });
  }

  /// 知道了弹框：一个确认按钮
  static showConfirm(BuildContext context,
      {@required String message,
      String knowText = "知道了",
      String imagePath = "",
      bool showCehckbox = false,
      onConfirm(bool checkboxValue)}) {
    showDialog(
        context: context,
        // barrierDismissible: false,
        builder: (BuildContext context) {
          return Scaffold(
            backgroundColor: Colors.transparent,
            body: BlocProvider(
              create: (context) => DialogUtilCubit(),
              child: BlocBuilder<DialogUtilCubit, DialogUtilState>(
                builder: (context, state) => Center(
                  child: Container(
                      // color: Colors.green,
                      child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Container(
                        margin: EdgeInsets.symmetric(vertical: 140.w),
                        padding: EdgeInsets.only(
                            left: 20.w, right: 20.w, bottom: 20.w),
                        width: 314.w,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10.w),
                        ),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            SizedBox(height: 60.w),
                            _text(message),
                            Container(
                              height: showCehckbox ? 68.h : 20.h,
                              // color: Colors.red,
                              child: showCehckbox
                                  ? Row(
                                      children: [
                                        Checkbox(
                                            value: state.checked,
                                            checkColor: Color(0xFFFFFFFF),
                                            activeColor: Color(0xFF969DA3),
                                            onChanged: (selected) {
                                              BlocProvider.of<DialogUtilCubit>(
                                                      context)
                                                  .onCheckChange();
                                            }),
                                        Expanded(
                                            child: Text(
                                          "下次不再提示",
                                          style: TextStyle(
                                              color: Color(0xFF595959),
                                              fontSize: 14.w),
                                        ))
                                      ],
                                    )
                                  : Container(),
                            ),
                            Container(
                              alignment: Alignment.center,
                              child: _button(knowText,
                                  width: 258.w,
                                  height: 44.w,
                                  selected: true, onTap: () {
                                Navigator.of(context).pop();
                                if (onConfirm != null) onConfirm(state.checked);
                              }),
                            )
                          ],
                        ),
                      ),
                      Positioned(
                        top: 97.5.w,
                        child: imagePath != ""
                            ? Container(
                                // color: Colors.orange,
                                alignment: Alignment.center,
                                child: Image.asset(imagePath,
                                    height: 86.w, width: 86.w),
                              )
                            : Container(),
                      ),
                      Positioned(
                        bottom: 0,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                          child: SvgPicture.asset(
                            AppResources.imagePath(
                              "svgs/close_pop.svg",
                            ),
                            color: Colors.white,
                            width: 45.w,
                            height: 45.w,
                          ),
                        ),
                      )
                    ],
                  )),
                ),
              ),
            ),
          );
        });
  }

  // 圆角-选中
  static BoxDecoration _boxRadius(bool isSelected) {
    return BoxDecoration(
        color: Color(isSelected ? 0xFFD6E600 : 0xFFFFFFFF),
        border: Border.all(color: Color(0xFFD6E600), width: 1),
        borderRadius: BorderRadius.all(Radius.circular(100)));
  }

  static Widget _button(String title,
      {double width = 78, double height, bool selected = false, onTap()}) {
    height = height ?? width;
    return GestureDetector(
      onTap: onTap,
      child: Container(
          width: width,
          height: height,
          alignment: Alignment.center,
          decoration: _boxRadius(selected),
          child: Text(title,
              style: TextStyle(
                  decoration: TextDecoration.none,
                  fontSize: 18.w,
                  color: Color(0xFF141413),
                  fontWeight: FontWeight.w400))),
    );
  }

  static Widget _text(String text) {
    return Text(text,
        strutStyle: StrutStyle(forceStrutHeight: true, height: 1, leading: 1.3),
        style: TextStyle(
            fontSize: 18.w,
            fontWeight: FontWeight.w400,
            color: Color(0xFF141413),
            decoration: TextDecoration.none));
  }
}

// --------------------
class DialogUtilCubit extends Cubit<DialogUtilState> {
  DialogUtilCubit() : super(DialogUtilState().init());

  // checkbox选中/未选中
  onCheckChange() {
    emit(state.clone()..checked = !state.checked);
  }
}

class DialogUtilState {
  bool checked;

  DialogUtilState init() {
    return DialogUtilState()..checked = false;
  }

  DialogUtilState clone() {
    return DialogUtilState()..checked = checked;
  }
}
